package edu.chl.dat255.bluebanana.logic.impl;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import edu.chl.dat255.bluebanana.logic.ICommunication;
import edu.chl.dat255.bluebanana.utils.PhoneCallReceiver;
import edu.chl.dat255.bluebanana.utils.SystemListener;

public class APCommunication implements ICommunication {
	private static ICommunication instance = new APCommunication();
	private static Context staticContext;
	
	private APCommunication() {
		// Singleton
	}

	public static ICommunication getInstance(Context context) {
		staticContext = context;
		return instance;
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setBlockCalls(boolean isBlockCalls) {
		PhoneCallReceiver.setBlockCalls(isBlockCalls);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setAutoRespond(boolean isAutoRespond) {
		SystemListener.setAutoRespond(isAutoRespond);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setAutoRespondMessage(String message) {
		SystemListener.setMessage(message);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setFlightMode(boolean isFlightMode) {
		Settings.System.putInt(
			      staticContext.getContentResolver(),
			      Settings.System.AIRPLANE_MODE_ON, isFlightMode ? 1 : 0);
		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        staticContext.sendBroadcast(intent);

	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setWifi(boolean isWifi) {
		WifiManager wifiManager = (WifiManager) staticContext.getSystemService(Context.WIFI_SERVICE);
		
		wifiManager.setWifiEnabled(isWifi);
		
		Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
        staticContext.sendBroadcast(intent);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setBluetooth(boolean isBluetooth) {
		BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (bluetoothAdapter == null) {
		    // Device does not support Bluetooth
		} else {
			if (isBluetooth) {
				bluetoothAdapter.enable();
			} else {
				bluetoothAdapter.disable();
			}
		}
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setPacketData(boolean isPacketData) {
//		WifiManager wifiManager = (WifiManager) staticContext.getSystemService(Context.CONNECTIVITY_SERVICE);
//		wifiManager.disconnect();
		
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public boolean isWifi() {
		WifiManager wifiManager = (WifiManager) staticContext.getSystemService(Context.WIFI_SERVICE);
		boolean b =WifiManager.WIFI_STATE_ENABLED == wifiManager.getWifiState();
		return b;
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public boolean isBluetooth() {
		BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (bluetoothAdapter == null) {
		    // Device does not support Bluetooth
			return false;
		} else {
			boolean b = BluetoothAdapter.STATE_ON == BluetoothAdapter.getDefaultAdapter().getState();
			return b;
		}	
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public boolean isFlightMode() {
		String compare = "Error";
		compare = Settings.System.getString(staticContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
		boolean b = compare.equals("airplane_mode_on");
		return b;
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public boolean isPacketData() {
		return true;
	}
}
